// GET
import axios from "axios";

export const getAllTasks = () => {
  return axios.get(
    '/task/AllTasks'
  ).then(res => {
    return res.data;
  })
}
// GET
export const getAllValidTasks = userId => {
  // 无参数
  return axios.get(
    '/task/ValidTasks',
      {
        params: {userId}
      }
  ).then(res => {
    return res.data;
  })
}

export const getTaskDetailsByTaskID = taskid => {
  // 参数：任务id
  return axios.get(
    `/task/detail/${taskid}`
  ).then(res => {
    return res.data;
  })
/*  return new Promise((resolve) => {
    console.log('start getting task detail: ')
    console.log(taskid)
    setTimeout(() => {
      console.log('task detail got!');
      resolve({
        "code": 200,
        "msg": "string",
        "data": {
          "username": "string",
          "id": 0,
          "userId": 0,
          "taskname": "string",
          "description": "string",
          "startTime": "2022-10-02 03:12:35",
          "deadline": "2022-10-02 03:12:35",
          "total_num": 0,
          "need_num": 0,
          "type": "function_test",
          "deviceRequirement": {
            osList: ['windowsXP', 'windows7'],
            minRam: 16,
            maxRam: 32,
            processorArchitectureList: ['x86']
          },
          "environment": "windowsXP",
          "capableToClaim": true
        }
      })
    }, 3000);
  })*/
}


export const createTaskFile = payload => {
  return axios.post('/taskfile/create', {
    taskId: payload.taskId,
    url: payload.url,
    type: payload.type,
    size: payload.size,
    kind: payload.kind,
    name: payload.name
  }).then(res => {
    return res.data;
  })
}

export const getTaskFile = payload => {
  // 参数：任务id
  const taskid = payload.taskId
  const kind = payload.kind
  return axios.get(
    `/taskfile/getfile/${taskid}/${kind}`
  ).then(res => {
    return res.data;
  })
}

// Post
export const deleteTaskByID = taskId => {
  return axios.post(
    `/task/delete/${taskId}`
  ).then(res => {
    return res.data;
  })
}

// get
// todo
export const getInvitedTaskByWorkerID = workerid => {
  return axios.get(
    `/user/getInvitedTasks/${workerid}`
  ).then(res => {
    return res.data;
  })
}

export const getTaskDeviceRequirements = (taskId) => {
/*  return new Promise((resolve) => {
    console.log('start getting task device requirements: ')
    console.log(taskId)
    setTimeout(() => {
      console.log('task device requirements got!');
      resolve({
        code: 200,
        msg: '',
        data: {
          osList: ['windowsXP', 'windows7'],
          minRam: 16,
          maxRam: 32,
          processorArchitectureList: ['x86']
        }
      })
    }, 3000);
  })*/
  return axios.get(
      `/task/deviceRequirements?taskId=${taskId}`
  ).then(res => {
    return res.data;
  });
}

export const getTaskWorkerDevices = (taskId, workerId) => {
/*  return new Promise((resolve) => {
    console.log('start getting task worker devices');
    console.log(taskId)
    console.log(workerId)
    setTimeout(() => {
      console.log('task worker devices got!');
      resolve({
        "code": 200,
        "msg": "string",
        "data": [
          {
            "id": 0,
            "name": "测试设备1",
            "os": "windows7",
            "ram": 16,
            "processorArchitecture": "x86"
          },
          {
            "id": 1,
            "name": "测试设备2",
            "os": "windowsXP",
            "ram": 24,
            "processorArchitecture": "x86"
          },
          {
            "id": 2,
            "name": "测试设备3",
            "os": "windows7",
            "ram": 32,
            "processorArchitecture": "x86"
          }
        ]
      })

    }, 3000)
  })*/
  return axios.get(
      `/task/workerDevices?taskId=${taskId}&workerId=${workerId}`
  ).then(res => {
    return res.data;
  });
}

// todo lyl params number
// 接口文档里只有一个参数，name = userId, description = 任务id
// 应该是两个id都要吧
export const loadWorkerListOfTask = payload => {
  const {userId,taskId} = payload
  return axios
      .get(`/task/workers`, {
        params: {userId,taskId}
      })
      .then(res => {
        return res.data;
      })
}


export const getCollaborationTaskGroups = taskId => {
  return axios
      .get('/task/collaborationTask/reportGroups', {
        params: {
          taskId
        }
      })
      .then(res => {
        return res.data;
      });

/*  return new Promise(resolve => {
    console.log('start getting collaboration task groups: ')
    console.log(taskId);
    setTimeout(() => {
      console.log('collaboration task groups got!');
      resolve({
        code: 200,
        msg: '',
        data: [
          [
            {
              "taskId": 0,
              "reportId": 1,
              "workerId": 0,
              "workerName": "string"
            },
            {
              "taskId": 1,
              "reportId": 2,
              "workerId": 0,
              "workerName": "string"
            },
            {
              "taskId": 2,
              "reportId": 3,
              "workerId": 0,
              "workerName": "string"
            }
          ],
          [
            {
              "taskId": 0,
              "reportId": 4,
              "workerId": 0,
              "workerName": "string"
            },
            {
              "taskId": 1,
              "reportId": 5,
              "workerId": 0,
              "workerName": "string"
            }
          ],
          [
            {
              "taskId": 0,
              "reportId": 6,
              "workerId": 0,
              "workerName": "string"
            }
          ]
        ]
      })
    }, 3000);
  })*/
}

export const getCollaborationTaskDetail = taskId => {
  return axios
      .get('/task/collaborationTask/detail', {
        params: {
          taskId
        }
      })
      .then(res => res.data);
/*  return new Promise(resolve => {
    console.log('start getting collaboration task detail: ')
    console.log(taskId);
    setTimeout(() => {
      console.log('collaboration task detail got!');
      resolve({
        code: 200,
        msg: '',
        data: {
          "id": 0,
          "username": "string",
          "need_num": 0,
          "taskname": "string",
          "description": "string",
          "type": "string",
          "deadline": "2022-10-26T12:59:57.407Z",
          "subTasks": [
            {
              "id": 0,
              "taskname": "string",
              "deviceRequirement": {
                "osList": [
                  "string"
                ],
                "minRam": 0,
                "maxRam": 0,
                "processorArchitectureList": [
                  "string"
                ]
              },
              "type": "string",
              "deadline": "2022-10-25T05:30:51.788Z",
              "description": "string",
              "reportPrice": 0,
              "dependencies": [

              ],
              "total_num": "string"
            },
            {
              "id": 1,
              "taskname": "string",
              "deviceRequirement": {
                "osList": [
                  "string"
                ],
                "minRam": 16,
                "maxRam": 32,
                "processorArchitectureList": [
                  "string"
                ]
              },
              "type": "string",
              "deadline": "2022-10-25T05:30:51.788Z",
              "description": "string",
              "reportPrice": 0,
              "dependencies": [
                0
              ],
              "total_num": "string"
            },
            {
              "id": 2,
              "taskname": "string",
              "deviceRequirement": {
                "osList": [
                  "string"
                ],
                "minRam": 0,
                "maxRam": 0,
                "processorArchitectureList": [
                  "string"
                ]
              },
              "type": "string",
              "deadline": "2022-10-25T05:30:51.788Z",
              "description": "string",
              "reportPrice": 0,
              "dependencies": [
                0
              ],
              "total_num": "string"
            },
            {
              "id": 3,
              "taskname": "string",
              "deviceRequirement": {
                "osList": [
                  "string"
                ],
                "minRam": 0,
                "maxRam": 0,
                "processorArchitectureList": [
                  "string"
                ]
              },
              "type": "string",
              "deadline": "2022-10-25T05:30:51.788Z",
              "description": "string",
              "reportPrice": 0,
              "dependencies": [
                1
              ],
              "total_num": "string"
            },
            {
              "id": 4,
              "taskname": "string",
              "deviceRequirement": {
                "osList": [
                  "string"
                ],
                "minRam": 0,
                "maxRam": 0,
                "processorArchitectureList": [
                  "string"
                ]
              },
              "type": "string",
              "deadline": "2022-10-25T05:30:51.788Z",
              "description": "string",
              "reportPrice": 0,
              "dependencies": [
                2, 3
              ],
              "total_num": "string"
            }
          ]
        }
      })
    }, 3000);
  })*/


}


export const createCollaborationTask = collaborationTask => {
  return axios.post('/task/collaborationTask/create', collaborationTask).then(res => res.data);
/*
  return new Promise(resolve => {
    console.log('start creating collaboration task: ')
    console.log(collaborationTask);
    setTimeout(() => {
      console.log('collaboration task created');
      resolve({
        "code": 0,
        "msg": "string",
        "data": {
          "id": 0,
          "username": "string",
          "need_num": 0,
          "taskname": "string",
          "description": "string",
          "type": "string",
          "deadline": "2022-11-12T09:43:10.302Z",
          "subTasks": [
            {
              "id": 0,
              "taskname": "string",
              "deviceRequirement": {
                "osList": [
                  "string"
                ],
                "minRam": 0,
                "maxRam": 0,
                "processorArchitectureList": [
                  "string"
                ]
              },
              "type": "string",
              "deadline": "2022-11-12T09:43:10.302Z",
              "description": "string",
              "reportPrice": 0,
              "dependencies": [
                0
              ],
              "total_num": "string"
            }
          ]
        }
      })
    }, 3000)
  })
*/

}

export const getCollaborationTaskMyGroup = (taskId, userId) => {
  return axios
      .get('/task/collaborationTask/myGroup', {
        params: {
          taskId,
          userId
        }
      })
      .then(res => res.data);
/*  return new Promise(resolve => {
    console.log('start getting collaboration task my group');
    console.log(`taskId: ${taskId}; userId: ${userId}`);
    setTimeout(() => {
      console.log('collaboration task my group got');
      resolve({
        "code": 200,
        "msg": "string",
        "data": [
          {
            "taskId": 1,
            "reportId": 0,
            "workerId": 2,
            "workerName": "string1"
          },
          {
            "taskId": 0,
            "reportId": 0,
            "workerId": 2,
            "workerName": "string0"
          },
          {
            "taskId": 2,
            "reportId": 0,
            "workerId": 2,
            "workerName": "string2"
          },
          {
            "taskId": 4,
            "reportId": 0,
            "workerId": 2,
            "workerName": "string3"
          }
        ]
      })
    })
  })*/
}

export const pickSubTask = (subTaskId, workerId) => {
  return axios.post('/task/collaborationTask/pickSubTask', {
    subTaskId,
    workerId
  }).then(res => res.data);
/*  return new Promise(resolve => {
    console.log('start pick sub task');
    console.log(`subTaskId: ${subTaskId}; workerId: ${workerId}`);
    setTimeout(() => {
      console.log('sub task picked');
      resolve({
        "code": 200,
        "msg": "string",
        "data": {}
      })
    }, 3000)
  })*/
}
